package com.mt.skyhoist.util.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.mt.skyhoist.R;

import com.mt.skyhoist.util.bean.DeviceModel;

import java.util.List;

public class DeviceGroupView extends LinearLayout {
    private Context context;

    public DeviceGroupView(Context context) {
        super(context);
        this.context = context;

        initView();
    }

    // 初始化控件
    private View array_v;
    private View name_btn;
    private TextView name_tv;
    private View set_v;

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.fmt_device_group, this);

        array_v = findViewById(R.id.array_v);
        name_btn = findViewById(R.id.name_btn);
        set_v = findViewById(R.id.set_v);
        name_tv = findViewById(R.id.name_tv);

        name_btn.setOnLongClickListener(myOnLongClickListener);

    }

    // 长按监听
    private OnLongClickListener myOnLongClickListener = new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            callback.onNameLongClick(index);
            return true;
        }
    };

    private int index;
    public void setData(List<DeviceModel> data, boolean expended, int index) {
        this.index = index;
        boolean noDevice = false;

        if ((data == null) || (data.size() == 0)) {  // 没有绑定过设备的情况下
            noDevice = true;
        }

        name_tv.setText("Station " + (this.index+1));  // 显示名字

        int array_id;
        int set_id;
        if(noDevice){
            set_id = R.drawable.circle_gray;
            if (expended) {
                array_id = R.drawable.up_gray_array;
            } else {
                array_id = R.drawable.down_gray_array;
            }
        } else {
            set_id = R.drawable.circle_green;
            if (expended) {
                array_id = R.drawable.up_orange_array;
            } else {
                array_id = R.drawable.down_orange_array;
            }
        }

        array_v.setBackgroundResource(array_id);
        set_v.setBackgroundResource(set_id);
    }

    private Callback callback;
    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public interface Callback {
        void onNameLongClick(int index);
    }

}
